media_reference = function () {
    var mr = this;
    this.target_div = "media_edit_area";
    this.content_area = "media_content_area";
};


media_control = function () {
    this.trash = function () {
        
    }
}

media_content = function () {
    var mc = this;
    var _mr = new media_reference ();
    this.loaded = false;
    this.target_div = _mr.target_div;
    this.content_area = _mr.content_area;
    
    this.load = function () {
        if (! this.loaded) {
            $("#" + this.target_div).append("<div id=" + this.content_area + "></div>");
            $("#" + this.content_area).css ({
                position: "absolute",
                display: "none",
                "background-color": "silver",
                "border-width":"2px",
                "border-style":"outset"
            });
            this.loaded = true;
        $.ajax({
            async: false,
            type: 'GET',
            url: "/modules/media/templates/form/index.html",
            success: function (result) {
                mc.form (result);
                $("#" + mc.content_area).css({"visibility": "visible"});
                $("#loaded_area").css({"visibility": "visible", "overflow": "auto","height":"200px","border":"1px solid"});
                $("#" + mc.content_area + "  div").css({"visibility": "visible","clear":"both"});
                //$("#" + mc.content_area).children().css({"visibility": "visible","clear":"both"});
                $("#" + mc.content_area).show("slow");
            }
        });            
        }  
    };
    
    this.form = function (html) {
        $("#" + mc.content_area).html (html);
        html = "<div>" + area + ": " + section + "</div>";
        html += "<div id=loaded_area>";
        $.ajax({
                    async: false,
                    type: 'POST',
                    url: "/modules/media/get_media_list.php",
                    datatype: "json",
                    data: {
                            area : area,
                            section : section
                        },
                    success: function (result) {
                      
                            var media_list = jQuery.parseJSON(result);
                            for(_obj in media_list) {
                                if ((typeof media_list[_obj] != "function")&&(media_list[_obj] != "undefined")) {
                                 html += "<div>" + media_list[_obj];
                                    if (media_list[_obj] != "No media found") {
                                        
                                        
                                         html +=  "<img class=\"btn_media_link\" style=\"float:right\" src=/modules/media/templates/form/images/link.png"
                                            +  " title=\"insert link to media\""
                                            +  " media_src=\"" + media_list[_obj] +"\""
                                            + ">";
                                    
                                       html +=  "<img class=\"btn_media_info\" style=\"float:right\" src=/modules/media/templates/form/images/information.png"
                                            +  " title=\"media information\""
                                            +  " media_src=\"" + media_list[_obj] +"\""
                                            + ">";
                                   
                                       html +=  "<img class=\"btn_media_delete\" style=\"float:right\" src=/modules/media/templates/form/images/folder_delete.png"
                                            +  " title=\"delete media\""
                                            +  " media_src=\"" + media_list[_obj] +"\""
                                            + ">";
                                    }
                                  html +=  "</div>";
                                }
                            }
                    }
         });
         html += "</div>";
         html +=  "<iframe style=\"height:60px;\" frameborder=0 src=\"/modules/media/upload_media.php?section=" +section+ "&area=" + area +"\"></iframe>";
         $("#" + mc.content_area).append (html);
         
         $(".btn_media_delete").click(function () {mc.trash($(this).attr("media_src"))});
         $(".btn_media_info").click(function () {mc.information($(this).attr("media_src"))});
         $(".btn_media_link").click(function () {mc.link_media($(this).attr("media_src"))});
    };
    
    this.link_media = function (src) {
        var seltext = (document.all)? document.selection.createRange() : document.getSelection();
        if (seltext == "") alert ("Please select some text for the link")
        else {
            src = "/media/" + area + "/" + section  + "/" + src;
            document.execCommand('createlink',false,src);
            alert ("link inserted");
        }

    }
    
    this.information = function (src) {
        prompt(src + " url...","/media/" + area + "/" + section + "/" + src);
    }
    
    this.trash = function (src) {
         var conf_message = "Delete " + area + "/" + section + "/" + src + "?"
         var conf = confirm (conf_message);
         if (conf) {
            $.ajax({
                    async: false,
                    type: 'POST',
                    url: "/modules/media/trash_media_item.php",
                    data: {
                            area : area,
                            section : section,
                            src : src},
                    success: function (result) {
                      alert(result);
                      $("#mm_open").click();
                    }
            });
         }
    }
    
    
    this.close = function () {
        $("#" + mc.content_area).hide("slow");
    };
    
};

media_manage = function () {
    var mm = this;
    var _mr = new media_reference ();
    this.target_div = _mr.target_div;
    
    this.initialize = function () {
        if ($("#" +this.target_div).length > 0) this.load();
        else alert (this.target_div + " is required");
    };
    
    this.load = function () {
        $.ajax({
            async: false,
            type: 'GET',
            url: "/modules/media/templates/menu/index.html",
            success: function (result) {mm.menu (result);}
        });  
    };
    
    this.menu  = function (html) {
        $("#" + mm.target_div).html (html);
    };
    
    this.initialize();
    
};


image_manager = function () {
    var im = this;
    this.selected_image = "";
    this.initialize = function () {
        $(document).ready (function () { 
            $(document).find("img").live("click", function () {
                var x = $(this).closest("div").attr("contenteditable");
                var y = $(this).parent().parent().attr("contenteditable");
                
                
                if (($(this).closest("div").attr("contenteditable"))||($(this).parent().parent().attr("contenteditable")))  {
                    im.selected_image = $(this).attr("src");
                } 
            });
        
        
  
        });
        $(document).change (function () { 
           //alert (2) 
        });
        
    };
    
    this.align = function (alignment) {
        if (this.selected_image == "") alert("please select an image") 
        else switch (alignment) {
            case "left":
                $("img[src='" + this.selected_image + "']").css ({
                    "float" : "left"
                });
                break;
            case "center":
                $("img[src='" + this.selected_image + "']").css ({
                    "float" : ""
                });
                break;
            case "right":
                $("img[src='" + this.selected_image + "']").css ({
                    "float" : "right"
                });
                break;      
        }
    }
    this.initialize();
}

